package vn.appsmobi.ui;

import vn.appsmobi.R;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.ulti.ImageLoadOption;
import vn.appsmobi.ulti.Log;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

public class ListAppListItem extends RelativeLayout
{
  protected AppInfo mAppInfo;	
  private TextView mTopAppName;
  private ImageView mTopAppPhoto;
  private TextView mTopAppAgency;
  private ActionButton topapp_button;
  private RatingBar rateImage;
  private Handler mHandler = new Handler();
  private Context _context;
  private int mPosition;
  
  public ListAppListItem(Context context, AttributeSet attr)
  {
    super(context, attr);
    _context = context;
  }

  public void bind(AppInfo appinfo)
  {
	this.mTopAppPhoto 	= ((ImageView)findViewById(R.id.topapp_photo));
    this.mTopAppName 	= ((TextView)findViewById(R.id.topapp_name));
    this.mTopAppAgency 	= ((TextView)findViewById(R.id.topapp_agencyname));
    this.topapp_button	= (ActionButton)findViewById(R.id.topapp_button);
    this.rateImage		= (RatingBar)findViewById(R.id.ratingbar);

  }
  
  
  public void rebind(AppInfo appinfo,int position)
  {
	  mPosition = position;	  
	  rebind(appinfo);
  }
  
  public void rebind(AppInfo appinfo)
  {
	  this.mAppInfo = appinfo;
	  this.mAppInfo.addUpdateListener(this.mUpdateListener);
	  updateViewContent(appinfo);
	  updateViewStatus(appinfo);
  }
  
  
  
  private AppInfo.AppInfoUpdateListener mUpdateListener = new AppInfo.AppInfoUpdateListener()
  {
    public void onContentUpdate(final AppInfo appinfo)
    {
    	ListAppListItem.this.mHandler.post(new Runnable()
	      {
	        public void run()
	        {
	        	Log.v("on updateViewContent:" + appinfo.getName());
	        	//Toast.makeText(ListAppListItem.this._context, "on updateViewContent:" + appinfo.getName(), Toast.LENGTH_LONG).show();
	        	ListAppListItem.this.updateViewContent(appinfo);
	        	ListAppListItem.this.mTopAppName.setText("tesst");
	        }
	      });
    }

    public void onStatusUpdate(final AppInfo appinfo)
    {
      ListAppListItem.this.mHandler.post(new Runnable()
      {
        public void run()
        {
        	Log.v("on updateStatus:" + appinfo.getName());
        	//Toast.makeText(ListAppListItem.this._context, "onStatusUpdate:" + appinfo.getName() , Toast.LENGTH_LONG).show();
        	ListAppListItem.this.updateViewStatus(appinfo);        	
        }
      });
    }
  };
  
  protected void updateViewContent(AppInfo appinfo)
  {
	  if(appinfo.getName().length()>15)
		{
			this.mTopAppName.setText(_context.getString(R.string.list_app_title,mPosition,appinfo.getName().substring(0,15)+"...") );
		}
		else
			this.mTopAppName.setText(_context.getString(R.string.list_app_title,mPosition,appinfo.getName()));
	    
	    ImageLoader.getInstance().displayImage(appinfo.getIconLink(), this.mTopAppPhoto,ImageLoadOption.options_icon);    
	    this.mTopAppAgency.setText(appinfo.getCompanyName());
	    //topapp_button.rebind(appinfo);
	    this.rateImage.setRating(appinfo.getRateNum());
  }
  
  protected void updateViewStatus(AppInfo appinfo)
  {	
    //if (!TextUtils.isEmpty(appinfo.getId()))
      this.topapp_button.rebind(appinfo);     
  }
  
  public void unbind()
  {
    this.mAppInfo.removeUpdateListener(this.mUpdateListener);
  }
  

}
